import React,{useEffect,useState} from 'react'

export default function TimerInterval() {
 const [num,setNum]=useState(0)
 useEffect(()=>{
    let timers=window.setInterval(()=>{
        console.log('-------定时器---------');
        setNum(arg=>arg+1)
    },1000)
    //return后面的回调函数就是在模拟componentWillUnMount的工作
    return ()=>{
       window.clearInterval(timers)
    }
  },[])
  return (
    <div>
        <h1>{num}</h1>
    </div>
  )
}
